package cn.longge.controller;

import cn.longge.entity.Result;
import cn.longge.entity.SysRole;
import cn.longge.entity.SysUser;
import cn.longge.service.ISysRoleService;
import cn.longge.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("manager/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    @RequestMapping("index")
    public ModelAndView index(){
        return new ModelAndView("role/role");
    }

    @RequestMapping("selectRoleAndOffice/{page}/{pageSize}")
    public Result selectRoleAndOffice(@PathVariable int page, @PathVariable int pageSize, @RequestBody Map<String,Object> params){
        PageInfo pageInfo = roleService.selectRoleAndOffice(page,pageSize,params);
        return new Result(true,"success",pageInfo);
    }

    @RequestMapping("toFenpei")
    public ModelAndView toFenpei(){
        return new ModelAndView("role/role-user");
    }

    @RequestMapping("getChoosed")
    public Result getChoosed(Integer rid){
        List<SysUser> sysUsers = userService.selectByRoleId(rid);
        return new Result(true,"success",sysUsers);
    }

    @RequestMapping("delByIds")
    public Result deleteByUserIds(long rid,Long[] ids){
        List<Long> longs = Arrays.asList(ids);
        int i = roleService.deleteByUserIds(rid, longs);
        if(i < 1){
            return new Result(false,"删除失败",null);
        }
        return new Result(true,"删除成功",null);
    }

    @RequestMapping("getUnChoose")
    public Result getUnChoose(Long oid,Long rid){
        List<SysUser> sysUsers = userService.selectByOfficeId(oid, rid);
        return new Result(true,"success",sysUsers);
    }

    @RequestMapping("addUsers")
    public Result addUsers(long rid,Long[] ids){
        List<Long> longs = Arrays.asList(ids);
        int i = roleService.insertMany(rid, longs);
        if(i < 1){
            return new Result(false,"fail",null);
        }
        return new Result(true,"success",null);

    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("role/role-save");
    }

    @RequestMapping("selectRole")
    public Result selectRole(Long rid){
        SysRole sysRole = new SysRole();
        sysRole.setId(rid);
        sysRole.setDelFlag("0");
        SysRole sysRole1 = roleService.selectOne(sysRole);
        return new Result(true,"success",sysRole1);
    }

    @RequestMapping("toSelect")
    public ModelAndView toSelect(){
        return new ModelAndView("role/role-select");
    }

    @RequestMapping("doUpdate")
    public Result doUpdate(@RequestBody SysRole role){
        int i = roleService.updateByPrimaryKeySelective(role);
        if(i < 1){
            return new Result(false,"更新失败",null);
        }
        return new Result(true,"更新成功",null);

    }
}
